<?php
/**
* File holding the user settings class
*
* @author       Fabrice Douteaud <admin@clearbudget.net>
* @package      framework
* @access       public
*/

/***********************************************************************

  Copyright (C) 2008  Fabrice Douteaud (admin@clearbudget.net)

    This file is part of ClearBudget.

    ClearBudget is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    ClearBudget is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with ClearBudget.  If not, see <http://www.gnu.org/licenses/>.


************************************************************************/

/**
* Class to retreive and set the user preferences. It is used in the context.
* 
* @author       Fabrice Douteaud <admin@clearbudget.net>
* @package      framework
* @access       public
*/
class class_settings {
  /**
	* @var object Holds the instance of the current class
	* @access private
	* @static  	
	*/
  private static $_instance = null;
  /**
	* @var object Holds an instance of the DB object
	* @access private	
	*/
  private $db = null;
  /**
	* @var string the user language 
	* @access private	
	*/
  private $language = null;
  /**
	* @var string the user username 
	* @access private	
	*/
  private $username = null;
  /**
	* @var string the user password 
	* @access private	
	*/
  private $password = null;
  /**
	* @var boolean flag telling if the site should be secured 
	* @access private	
	*/
  private  $secureAccess = null;
  /**
	* @var string the user currency 
	* @access private	
	*/
  private $currency = null;
  /**
	* @var integer the initial balance 
	* @access private	
	*/
  private $initialBalance = null;
  /**
	* @var boolean flag telling if the user preferences have been loaded already 
	* @access private	
	*/
  private $settingsLoaded = false;
  
  /**
	* Saves the language preference 
	*  	
	* @param string the language
	* @return void
	* @access public	
	*/		
  public function setLanguage($lang) {
    $this->db->addFields('language', $lang);
    $this->db->addWhere('id', '=', '1');
    // do the update
    $this->db->update('t_settings');
    // set the internal var
    $this->language = $lang;
    }
  
  /**
	* Saves all the user preferences at once 
	*
	*  	
	* @param boolean flag telling if the settings are sufficient to say that the application is fully setup
	* @return void
	* @access public	
	*/		
  public function updateUserSettings($isNewUser = false) {
    $doUpdate = false;
    if($this->username!=null) {
      $this->db->addFields('username', $this->username);
      $doUpdate = true;
      }
    if($this->password!=null) {
      $this->db->addFields('password', md5($this->password));
      $doUpdate = true;
      }
    if($this->secureAccess!=null) {
      $this->db->addFields('secureAccess', $this->secureAccess);
      $doUpdate = true;
      }
    if($this->language!=null) {
      $this->db->addFields('language', $this->language);
      $doUpdate = true;
      }
    if($this->currency!=null) {
      $this->db->addFields('currency', $this->currency);
      $doUpdate = true;
      }
    if($this->initialBalance!=null) {
      $this->db->addFields('initialBalance', $this->initialBalance);
      $doUpdate = true;
      }
    
    if($doUpdate) {
      $this->db->addWhere('id', '=', '1');
      // do the update
      if($isNewUser) $this->db->insert('t_settings');
      else $this->db->update('t_settings');
      
      // reset the internal flag
      $this->settingsLoaded = false;
      // load the new user settings
      $this->getSettings();
      }
    }

  /**
	* Reset the password to null
	*
	* @return void
	* @access public	
	*/		
  public function resetPassword() {
    $this->db->addFields('password', '');
    $this->db->addWhere('id', '=', '1');
    $this->db->update('t_settings');
    }
    
  /**
	* Insert a new user in the DB 
	*
	*  	
	* @param string the username
	* @param string the password
	* @return void
	* @access public	
	*/		
  public function insertUser($username=null, $password=null) {
    if($username == null || $username = '') {
      return false;
      }
    if($password == null || $password = '') {
      return false;
      }
    $this->db->addFields('username', $username);
    $this->db->addFields('password', md5($password));
    // do the insert
    $userId = $this->db->insert('t_settings');
    return $userId;
    }
    
  /**
	* Gets all the user preferences 
	* 	
	* @return void
	* @access public	
	*/		
  public function getSettings() {
    class_debug::addMsg(__FILE__, 'Getting user settings', DEBUGDEBUG);
    if($this->settingsLoaded) {
      class_debug::addMsg(__FILE__, 'User settings already loaded', DEBUGDEBUG);
      return;
      }
    
    // set the fields to retreive
    $this->db->addFields('language');
    $this->db->addFields('username');
    $this->db->addFields('password');
    $this->db->addFields('secureAccess');
    $this->db->addFields('currency');
    $this->db->addFields('initialBalance');
    $this->db->addWhere('id', '=', '1');
    // run the query
    $this->db->select('v_settings');
    // since there is only one row and since the view can return only one row, we fetch only one row
    $item = $this->db->fetchRow();
    if($item == false) {
      class_debug::addMsg(__FILE__, 'No user setting yet - application is not setup', DEBUGINFO);
      return false;
      }
    $this->language = (string)$item['language'];
    $this->username = (string)$item['username'];
    $this->password = (string)$item['password'];
    $this->secureAccess = (string)$item['secureAccess'];
    $this->currency = (string)$item['currency'];
    $this->initialBalance = (integer)$item['initialBalance'];
    $this->settingsLoaded = true;
    class_debug::addMsg(__FILE__, 'User settings loaded', DEBUGINFO);
    return true;
    }

  /**
	* Gets the username 
	*  		
	* @return void
	* @access public	
	*/		
  public function getUsername($fromDB = false) {
    if(!$fromDB) return $this->username;
    // set the fields to retreive
    $this->db->addFields('username');
    $this->db->addWhere('id', '=', '1');
    // run the query
    $this->db->select('v_settings');
    $item = $this->db->fetchRow();
    if($item == false) {
      return false;
      }
    return (string)$item['username'];
    }

  /**
	* Gets the password 
	*  		
	* @return void
	* @access public	
	*/		
  public function getPassword($fromDB = false) {
    if(!$fromDB) return $this->password;
    // set the fields to retreive
    $this->db->addFields('password');
    $this->db->addWhere('id', '=', '1');
    // run the query
    $this->db->select('v_settings');
    $item = $this->db->fetchRow();
    if($item == false) {
      return false;
      }
    return (string)$item['password'];
    }
  
  /**
	* verify username/password 
	*  		
	* @return void
	* @access public	
	*/		
  public function checkCredentials($username, $password) {
    class_debug::addMsg(__FILE__, 'Checking credential for '.$username.', '.$password, DEBUGDEBUG);
    $this->db->addFields('id');
    // set the fields to retreive
    $this->db->addWhere('username', '=', $username);
    $this->db->addWhere('password', '=', md5($password));
    $this->db->addWhere('id', '=', '1');
    // run the query
    $this->db->select('v_settings');
    $item = $this->db->fetchRow();
    if($item == false) {
      class_debug::addMsg(__FILE__, 'Credentials are not valid', DEBUGDEBUG);
      return false;
      }
    class_debug::addMsg(__FILE__, 'Credentials are valid', DEBUGDEBUG);
    return true;
    }
  
  /**
	* Returns the currency 
	*  	
	*
	* @return string currency
	* @access public	
	*/		
  public function getCurrency() {
    return $this->currency;
    }
  
  /**
	* Returns the language 
	*  	
	*
	* @return string language
	* @access public	
	*/		
  public function getLanguage() {
    return $this->language;
    }
  
  /**
	* Returns the initial Balance 
	*  	
	*
	* @return integer initial Balance
	* @access public	
	*/		
  public function getInitialBalance() {
    return $this->initialBalance;
    }
  
  /**
	* tells if access to the application needs to be secure 
	*  	
	*
	* @return boolean secure access flag
	* @access public	
	*/		
  public function secureAccess() {
    return $this->secureAccess;
    }
  
  /**
	* magic method to access the private class variable 
	*     		
	* @return void
	* @access public
	*/
  public function __set($field, $value) {
    $this->$field = $value;
    return null;
    }
    
  /**
	* Provide an instance of the current class 
	*	
	* Implementation of the singleton pattern
	*   		
	* @return object An Instance of this class
	* @access public	
	*/  
  public static function getInstance() {
    if(is_null(self::$_instance)) {
      self::$_instance = new self();
      }
    return self::$_instance;
    } // getInstance()

  /**
	* class constructor 
	*	
	* access to this class is private as to implement the singleton pattern
	*   	
	* @return void
	* @access private
	*/
  private function __construct() {
    $this->db = class_db::getInstance();
    $this->getSettings();
    } // __construct()
  }
?>